<template>
  <div v-if="show" class="modal-overlay" @click="close">
    <div class="settings-modal" @click.stop>
      <div class="modal-header">
        <h3>系统设置</h3>
        <button @click="close" class="close-btn">
          <i class="fas fa-times"></i>
        </button>
      </div>
      
      <div class="modal-content">
        <!-- 界面设置 -->
        <div class="settings-section">
          <h4>界面设置</h4>
          
          <div class="setting-item">
            <div class="setting-info">
              <div class="setting-title">护眼模式</div>
              <div class="setting-desc">降低屏幕亮度，减少蓝光，保护视力</div>
            </div>
            <label class="toggle-switch">
              <input type="checkbox" v-model="localSettings.eyeCareMode" @change="updateSetting('eyeCareMode')">
              <span class="slider"></span>
            </label>
          </div>
          
          <div class="setting-item">
            <div class="setting-info">
              <div class="setting-title">深色模式</div>
              <div class="setting-desc">使用深色主题，适合夜间使用</div>
            </div>
            <label class="toggle-switch">
              <input type="checkbox" v-model="localSettings.darkMode" @change="updateSetting('darkMode')">
              <span class="slider"></span>
            </label>
          </div>

        </div>
        
        <!-- 功能设置 -->
        <div class="settings-section">
          <h4>功能设置</h4>
          
          <div class="setting-item">
            <div class="setting-info">
              <div class="setting-title">自动保存</div>
              <div class="setting-desc">自动保存编辑的内容</div>
            </div>
            <label class="toggle-switch">
              <input type="checkbox" v-model="localSettings.autoSave" @change="updateSetting('autoSave')">
              <span class="slider"></span>
            </label>
          </div>
          
          <div class="setting-item">
            <div class="setting-info">
              <div class="setting-title">通知提醒</div>
              <div class="setting-desc">接收系统通知和消息提醒</div>
            </div>
            <label class="toggle-switch">
              <input type="checkbox" v-model="localSettings.notifications" @change="updateSetting('notifications')">
              <span class="slider"></span>
            </label>
          </div>
          
          <div class="setting-item">
            <div class="setting-info">
              <div class="setting-title">音效提示</div>
              <div class="setting-desc">调节音效强度 (关闭/1档/2档/3档)</div>
            </div>
            <div class="audio-level-control">
        <button
          v-for="level in [0, 1, 2, 3]"
          :key="level"
          :class="['level-btn', { active: localSettings.audioLevel === level }]"
          class="no-audio"
          @click="setAudioLevel(level)"
        >
          {{ level === 0 ? '关闭' : level }}
        </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { setAutoSaveEnabled, isAutoSaveEnabled } from '../../utils/tools/auth.js'
import { setAudioLevel as setGlobalAudioLevel, getAudioLevel, getAudioSettings, AudioEffects } from '../../utils/tools/audio.js'

// Props
const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  settings: {
    type: Object,
    default: () => ({
      eyeCareMode: false,
      darkMode: false,
      compactMode: false,
      autoSave: true,
      notifications: true,
      audioLevel: 0
    })
  }
})

// 默认设置
const defaultSettings = {
  eyeCareMode: false,
  darkMode: false,
  compactMode: false,
  autoSave: true,
  notifications: true,
  audioLevel: 0
}

// Emits
const emit = defineEmits(['close', 'update-setting', 'reset-settings'])

// 本地设置副本
const localSettings = ref({ 
  ...props.settings,
  // 从auth.js加载自动保存设置
  autoSave: isAutoSaveEnabled(),
  // 从audio.js加载音效档位设置
  audioLevel: getAudioLevel()
})

// 监听props变化
watch(() => props.settings, (newSettings) => {
  localSettings.value = { ...newSettings }
}, { deep: true })

// 设置工具方法
function applySettings(settings) {
  const body = document.body
  const root = document.documentElement
  
  // 先清除所有模式类
  body.classList.remove('eye-care-mode', 'dark-mode', 'compact-mode')
  
  // 允许护眼模式和深色模式同时使用
  if (settings.eyeCareMode && settings.darkMode) {
    // 同时开启护眼模式和深色模式
    body.classList.add('eye-care-mode', 'dark-mode')
    root.setAttribute('data-theme', 'eye-care-dark')
    // 护眼深色模式：在深色基础上应用护眼滤镜
    root.style.setProperty('--bg-primary', '#2d1b0e')
    root.style.setProperty('--bg-secondary', '#3d2815')
    root.style.setProperty('--bg-tertiary', '#4d331c')
    root.style.setProperty('--text-primary', '#f4e6d7')
    root.style.setProperty('--text-secondary', '#d4c4b0')
    root.style.setProperty('--text-muted', '#b8a896')
    root.style.setProperty('--border-color', '#4d331c')
    root.style.setProperty('--accent-color', '#d4a574')
    root.style.setProperty('--accent-hover', '#c19a5c')
  } else if (settings.eyeCareMode) {
    // 仅护眼模式
    body.classList.add('eye-care-mode')
    root.setAttribute('data-theme', 'eye-care')
    // 重置为护眼模式变量
    root.style.setProperty('--bg-primary', '#fef7e6')
    root.style.setProperty('--bg-secondary', '#f3e8d3')
    root.style.setProperty('--bg-tertiary', '#e5d4b1')
    root.style.setProperty('--text-primary', '#6b4513')
    root.style.setProperty('--text-secondary', '#8b5a2b')
    root.style.setProperty('--text-muted', '#a68b5b')
    root.style.setProperty('--border-color', '#e5d4b1')
    root.style.setProperty('--accent-color', '#d4a574')
    root.style.setProperty('--accent-hover', '#c19a5c')
  } else if (settings.darkMode) {
    // 仅深色模式
    body.classList.add('dark-mode')
    root.setAttribute('data-theme', 'dark')
    // 使用CSS变量系统
    root.style.setProperty('--bg-primary', '#1f2937')
    root.style.setProperty('--bg-secondary', '#374151')
    root.style.setProperty('--bg-tertiary', '#4b5563')
    root.style.setProperty('--text-primary', '#f9fafb')
    root.style.setProperty('--text-secondary', '#d1d5db')
    root.style.setProperty('--text-muted', '#9ca3af')
    root.style.setProperty('--border-color', '#4b5563')
    root.style.setProperty('--accent-color', '#3b82f6')
    root.style.setProperty('--accent-hover', '#2563eb')
  } else {
    // 浅色模式
    root.setAttribute('data-theme', 'light')
    // 重置为默认值
    root.style.setProperty('--bg-primary', '#ffffff')
    root.style.setProperty('--bg-secondary', '#f8fafc')
    root.style.setProperty('--bg-tertiary', '#f3f4f6')
    root.style.setProperty('--text-primary', '#1f2937')
    root.style.setProperty('--text-secondary', '#6b7280')
    root.style.setProperty('--text-muted', '#9ca3af')
    root.style.setProperty('--border-color', '#e5e7eb')
    root.style.setProperty('--accent-color', '#4f46e5')
    root.style.setProperty('--accent-hover', '#4338ca')
  }
  
  // 紧凑模式可以与其他模式共存
  if (settings.compactMode) {
    body.classList.add('compact-mode')
  } else {
    body.classList.remove('compact-mode')
  }
}

function saveSettings(settings) {
  try {
    localStorage.setItem('kms-settings', JSON.stringify(settings))
  } catch (error) {
    console.warn('Failed to save settings:', error)
  }
}

function loadSettings() {
  try {
    const saved = localStorage.getItem('kms-settings')
    if (saved) {
      return { ...defaultSettings, ...JSON.parse(saved) }
    }
  } catch (error) {
    console.warn('Failed to load settings:', error)
  }
  return { ...defaultSettings }
}

// 组件方法
function close() {
  emit('close')
}

function updateSetting(settingKey) {
  const newSettings = { ...localSettings.value }
  
  // 如果更新的是自动保存设置，同步到auth.js
  if (settingKey === 'autoSave') {
    setAutoSaveEnabled(localSettings.value[settingKey])
  }
  
  // 如果更新的是音效设置，同步到audio.js
  if (settingKey === 'audioLevel') {
    setGlobalAudioLevel(localSettings.value[settingKey])
  }
  
  saveSettings(newSettings)
  applySettings(newSettings)
  emit('update-setting', settingKey, localSettings.value[settingKey])
}

function reset() {
  const resetSettings = { ...defaultSettings }
  
  // 重置auth.js中的自动保存设置
  setAutoSaveEnabled(resetSettings.autoSave)
  
  // 重置audio.js中的音效设置
  setGlobalAudioLevel(0)
  
  saveSettings(resetSettings)
  applySettings(resetSettings)
  localSettings.value = { ...resetSettings }
  emit('reset-settings')
}

// 设置音效档位
function setAudioLevel(level) {
  // 先更新全局音效设置
  setGlobalAudioLevel(level)
  // 再更新本地设置
  localSettings.value.audioLevel = level
  
  // 播放测试音效，使用新档位设置
  if (level > 0) {
    AudioEffects.click()
  }
  
  emit('update-setting', 'audioLevel', level)
}
</script>

<style scoped>
/* 设置弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.settings-modal {
  background: var(--bg-primary);
  color: var(--text-primary);
  border-radius: 0.75rem;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem;
  border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.25rem;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 0.375rem;
  transition: all 0.2s;
}

.close-btn:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.modal-content {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
}

.settings-section {
  margin-bottom: 2rem;
}

.settings-section:last-child {
  margin-bottom: 0;
}

.settings-section h4 {
  margin: 0 0 1rem 0;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 0;
  border-bottom: 1px solid var(--border-color);
}

.setting-item:last-child {
  border-bottom: none;
}

.setting-info {
  flex: 1;
  margin-right: 1rem;
}

.setting-title {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 0.25rem;
}

.setting-desc {
  font-size: 0.75rem;
  color: var(--text-secondary);
  line-height: 1.4;
}

/* 开关按钮样式 */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--bg-tertiary);
  transition: 0.3s;
  border-radius: 24px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: 0.3s;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

input:checked + .slider {
  background-color: var(--accent-color);
}

input:checked + .slider:before {
  transform: translateX(20px);
}

/* 音效档位控制样式 */
.audio-level-control {
  display: flex;
  gap: 0.5rem;
}

.level-btn {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border-color);
  background: var(--bg-primary);
  color: var(--text-primary);
  border-radius: 0.375rem;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  transition: all 0.2s;
  min-width: 3rem;
}

.level-btn:hover {
  background: var(--bg-tertiary);
  border-color: var(--accent-color);
}

.level-btn.active {
  background: var(--accent-color);
  color: white;
  border-color: var(--accent-color);
}

.level-btn.active:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}


</style>
